package com.zhaozhaonews.article.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhaozhaonews.article.mapper.ApArticleMapper;
import com.zhaozhaonews.article.service.ApArticleConfigService;
import com.zhaozhaonews.article.service.ApArticleContentService;
import com.zhaozhaonews.article.service.ApArticleService;
import com.zhaozhaonews.article.service.ArticleFreemarkerService;
import com.zhaozhaonews.common.constants.ArticleConstants;
import com.zhaozhaonews.model.article.dto.ArticleDto;
import com.zhaozhaonews.model.article.dto.ArticleHomeDto;
import com.zhaozhaonews.model.article.entity.ApArticle;
import com.zhaozhaonews.model.article.entity.ApArticleConfig;
import com.zhaozhaonews.model.article.entity.ApArticleContent;
import com.zhaozhaonews.model.common.dto.ResponseResult;
import com.zhaozhaonews.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author WanJL
 * @since 2025-02-17
 */
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {
    public static final Integer MAX_PAGE_SIZE=50;

    @Resource
    private ApArticleMapper articleMapper;

    @Resource
    private ApArticleConfigService apArticleConfigService;

    @Resource
    private ApArticleContentService apArticleContentService;


    @Resource
    private ArticleFreemarkerService articleFreemarkerService;

    /**
     * 加载新闻
     *
     * @return
     */
    @Override
    public List<ApArticle> load(Short loadType, ArticleHomeDto dto) {
        //1、校验参数
        Integer size = dto.getSize();
        if (size == null || size <= 0) {
            size=10;
        }
        //最大加载多少页
        size=Math.min(size,MAX_PAGE_SIZE);
        dto.setSize(size);

        //类型参数的校验
        if (!loadType.equals(ArticleConstants.LOADTYPE_LOAD_MORE)&&!loadType.equals(ArticleConstants.LOADTYPE_LOAD_NEW)){
            loadType=ArticleConstants.LOADTYPE_LOAD_MORE;
        }
        //新闻频道的校验
        if (StringUtils.isEmpty(dto.getTag())){
            dto.setTag(ArticleConstants.DEFAULT_TAG);
        }
        //时间校验
        if (dto.getMaxBehotTime()==null) dto.setMaxBehotTime(new Date());
        if (dto.getMinBehotTime()==null) dto.setMinBehotTime(new Date());

        //2、查询数据
        List<ApArticle> list= articleMapper.loadArticleList(dto,loadType);

        //3、封装结果
        return list;
    }

    @Override
    public ResponseResult saveArticle(ArticleDto dto) {
        /*强制当前线程睡眠3000毫秒，模拟服务降级的情况*/
      /*  try {
            Thread.sleep(3000);
        }catch (Exception e){
            e.printStackTrace();
        }*/


        //1、检查参数
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //把dto里的属性复制到apArticle
        ApArticle apArticle=new ApArticle();
        apArticle.setPublishTime(new Date());
        BeanUtils.copyProperties(dto,apArticle);
        //2、判断是否存在id，如果存在就是修改，如果不存在就是新增。
        if (dto.getId()==null){
            //1-保存新闻
            save(apArticle);
            //2-保存新闻配置
            ApArticleConfig apArticleConfig=new ApArticleConfig(apArticle.getId());
            apArticleConfigService.save(apArticleConfig);
            //3-保存新闻内容
            ApArticleContent apArticleContent=new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
            apArticleContentService.save(apArticleContent);
        }else {
            //1-修改新闻
            updateById(apArticle);
            //2-修改新闻内容
            ApArticleContent apArticleContent = apArticleContentService
                    .lambdaQuery()
                    .eq(ApArticleContent::getArticleId, dto.getId())
                    .one();
            apArticleContent.setContent(dto.getContent());
            apArticleContentService.updateById(apArticleContent);
        }
        //调用生成静态文件到MinIO中
        articleFreemarkerService.buildArticleToMinIO(apArticle,dto.getContent());


        //3、返回结果 新闻的id
        return ResponseResult.okResult(apArticle.getId());
    }
}
